﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Threading;
using Citi.RoutineBase;

namespace Citi.RoutineTest2
{
    [Serializable]
    public class Class1 : Citi.RoutineBase.IExecutableRoutine
    {
        public string Teste = "unchanged";

        public void Rollback(ref RoutineBase.ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            currentAction.Description = Teste;
        }
        
        public IEnumerable<RoutineParameter> Parameters { get
        {
            return  new List<RoutineParameter>
                {
                    new RoutineParameter{ Description = "Name of user", Name = "NameOfUser", Nullable = false, Required = true, Type = typeof(string)},
                    new RoutineParameter{ Description = "Number of user", Name = "NumberOfUser", Nullable = true, Required = false, Type = typeof(int)}
                };
        } } 

        public void Start(DateTime scheduledDate, ref RoutineBase.ICurrentAction currentAction, Dictionary<string, object> parameters)
        {
            Teste = "changed";

            //Helpers.ExcelHelper.RefreshAllPivotTable(@"c:\temp\testepivot.xlsx");

            try
            {
                currentAction.Description = "Starting";

                currentAction.StartLog("Extrating information");

                var startDate = DateTime.Now;

                while ((DateTime.Now - startDate).TotalMinutes < 15)
                    Thread.Sleep(1000);

                currentAction.StopLog();

                var test = ConfigurationManager.AppSettings["Test"];
                currentAction.Description = test;

                //var teste = new RoutineTest.Class3().Start(scheduledDate,ref currentAction);

                if (string.IsNullOrEmpty(test)) throw new KeyNotFoundException();

                File.Create(@"c:\temp\" + test + ".txt");
            }
            catch (DirectoryNotFoundException e)
            {
                throw new Exception("Directory does not exist. Path/.");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Stop(ref RoutineBase.ICurrentAction currentAction, Dictionary<string, object> parameters)
        {

        }
    }
}
